package com.bawei.newsapplication.base
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

/**
 * 作者: 谢庆平
 * 时间: 2024/10/15 16:58
 * 描述:
 */
abstract class BaseActivity<VM:ViewModel,DB:ViewDataBinding>(private var layoutId:Int) : AppCompatActivity() {
    lateinit var mViewModel: VM

    lateinit var mDataBinding:DB

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_base2)
        mDataBinding=DataBindingUtil.setContentView(this,layoutId)
        mDataBinding.lifecycleOwner=this

        var type=javaClass.genericSuperclass as ParameterizedType
        var classVM=type.actualTypeArguments[0] as Class<VM>
        mViewModel=ViewModelProvider(this)[classVM]

        initView()
        dataObserve()
    }

    abstract fun initView()

    abstract fun dataObserve()

    override fun onDestroy() {
        super.onDestroy()
        if(mDataBinding!=null){
            mDataBinding.unbind()
        }
    }
}